package twelve.reflection;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

@SuppressWarnings("all")
public class TestReflection {
    public static void main(String[] args) {
        String path = "twelve.reflection.User";
        try {
            Class clazz = Class.forName(path);
            System.out.println(clazz.hashCode());
            Class clazz2 = Class.forName(path);
            System.out.println(clazz2.hashCode());

            Class strClazz = String.class;
            Class strClazz2 = path.getClass();
            System.out.println(strClazz == strClazz2);

            Class intClass = int.class;

            int[] arr01 = new int[10];
            int[] arr02 = new int[30];
            int[][] arr03 = new int[30][3];

            System.out.println(arr01.getClass().hashCode());
            System.out.println(arr02.getClass().hashCode());
            System.out.println(arr03.getClass().hashCode());

            Class clazz3 = Class.forName(path);
            System.out.println(clazz3.getName());
            System.out.println(clazz3.getSimpleName());

//            Field[] fields = clazz3.getFields();
            Field[] fields = clazz3.getDeclaredFields();
            Field f = clazz3.getDeclaredField("uname");
            System.out.println(fields.length);
            for (Field field : fields) {
                System.out.println("属性: " + field);
            }

            Method[] method = clazz3.getDeclaredMethods();
            Method m01 = clazz3.getDeclaredMethod("getUname", null);
            Method m02 = clazz3.getDeclaredMethod("setUname", String.class);
            for (Method method1 : method) {
                System.out.println("方法: " + method1);
            }

            Constructor[] constructors = clazz3.getDeclaredConstructors();
            Constructor c = clazz3.getDeclaredConstructor(null);
//            Constructor c = clazz3.getDeclaredConstructor(int.class, int.class, String.class);
            System.out.println("获得构造器: " + c);
            for (Constructor constructor : constructors) {
                System.out.println("构造器: " + constructor);
            }

            User u = (User) clazz.newInstance();
            System.out.println(u);

            Constructor<User> c1 = clazz3.getDeclaredConstructor(int.class, int.class, String.class);
            User u2 = c1.newInstance(1001, 18, "高淇二");
            System.out.println(u2.getUname());

            User u3 = (User) clazz.newInstance();
            Method method1 = clazz.getDeclaredMethod("setUname", String.class);
            method1.invoke(u3, "高淇三");
            System.out.println(u3.getUname());

            User u4 = (User) clazz.newInstance();
            Field f1 = clazz.getDeclaredField("uname");
            f1.setAccessible(true);
            f1.set(u4, "高淇四");
            System.out.println(u4.getUname());
            System.out.println(f.get(u4));

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
